<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use  App\Models\User;
use  App\Models\UserGroup;
use  App\Models\WeixinFriendList;
use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;


class UserController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('is_open','str','group_id');
        $validator = Validator::make($input, [        
            'is_open'     =>  'nullable',
            'str'         =>  'nullable',
            'group_id'    =>  'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $user = User::where('id','>',0);
        if(isset($input['is_open'])){
            $user->where('is_open',$input['is_open']); 
        }
        if(isset($input['str'])){
            $user->where('name',$input['str'])->orwhere('phone',$input['str'])->orwhere('nickname',$input['str']); 
        }
        if(isset($input['group_id'])){
            $user->where('group_id',$input['group_id']); 
        }
        $group = UserGroup::get();
        $data = $user->get();
        $group_data = $this->formatGroup($group);
        foreach ($data as $key => $value) {
            $data[$key]['group_name'] = '';
            $data[$key]['creator_name'] = '';
            if($value['group_id']){
                $data[$key]['group_name'] = isset($group_data[$value['group_id']]['p_name'])?$group_data[$value['group_id']]['p_name'].'/'.$group_data[$value['group_id']]['name']:'';
            }
            if($value['creator']){

            }
        }
        return $this->returnOk($data);
    }

    protected function formatGroup($data)
    {
        $temp = [];
        foreach ($data as $key => $value) {
            $temp[$value['id']]['name'] = $value['name'];
            $temp[$value['id']]['p_name'] = '';
            if($value['pid']){
                foreach ($data as $k => $val) {
                    if($val['id']==$value['pid']){
                        $temp[$value['id']]['p_name'] = $val['name'];
                    }
                }
            }
        }
        return $temp;
    }

    public function create(Request $request)
    {
        $input = Input::only('phone','pw','name','nickname','group_id','remark');
        $validator = Validator::make($input, [        
            'phone' => 'required',
            'pw' => 'required',
            'name' => 'required',
            'nickname' => 'required',
            'group_id' => 'required',
            'remark' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $data = $input;
        $where['name'] = $input['name'];
        $exist = User::where($where)->first();
        if($exist){
            return $this->returnError([],409,'用户已存在');
        }else{
            $data['pw'] = md5($data['pw']);
            $data['is_open'] = 1;
            $data['creator_id'] = 0;
            $data['remark'] = $data['remark']??'';
            $group = UserGroup::where('id',$input['group_id'])->first();
            $data['model'] = $group['model'];
            User::create($data);
        }
        return $this->returnOk();
    }

    public function update(Request $request)
    {
        $input = Input::only('id','pw','name','nickname','group_id','remark','is_open');
        $validator = Validator::make($input, [        
            'id'      => 'required|integer',
            'name' => 'required',
            'nickname' => 'nullable',
            'group_id' => 'nullable',
            'remark' => 'nullable',
            'is_open' => 'nullable',
            'pw' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $user = User::where('id',$input['id'])->first();
        if($user){
            $exist = User::where('name',$input['name'])->where('id','!=',$input['id'])->first();
            if($exist){
                return $this->returnError([],409,'用户已存在');
            }
            $user->name = isset($input['name'])?$input['name']:$user->name;
            $user->nickname = isset($input['nickname'])?$input['nickname']:$user->nickname;
            $user->group_id = isset($input['group_id'])?$input['group_id']:$user->group_id;
            $user->remark = isset($input['remark'])?$input['remark']:$user->remark;
            $user->is_open = isset($input['is_open'])?$input['is_open']:$user->is_open;
            $user->pw = isset($input['pw'])?md5($input['pw']):$user->pw;
            $user->save();
        }else{
            return $this->returnError([],409,'用户不存在');
        }
        return $this->returnOk();
    }

    public function delete(Request $request)
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [   
            'id'      => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = User::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'用户不存在');
        }
        return $this->returnOk();
    }

    public function updateModel(Request $request)
    {
        $input = Input::only('id','model');
        $validator = Validator::make($input, [        
            'id' => 'required',
            'model' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $data = $input;
        $where['id'] = $input['id'];
        $exist = User::where($where)->first();
        if($exist){
            $exist->model = $input['model'];
        }else{
            return $this->returnError([],409,'用户不存在');
        }
        return $this->returnOk();
    }

}